package com.xcx.robot.util;

import com.xcx.robot.annotation.ExceptionCatch;
import org.springframework.stereotype.Component;
import ws.schild.jave.Encoder;
import ws.schild.jave.MultimediaObject;
import ws.schild.jave.encode.AudioAttributes;
import ws.schild.jave.encode.EncodingAttributes;

import java.io.File;

/**
 * @Author: 邢晨旭
 * @Date: 2021/7/7 16:45
 * @Description:
 */
@Component
@ExceptionCatch
public class Wav2Amr {
    public File turn(File source) throws Exception {
        File target = new File("test.amr");
        //Audio Attributes
        AudioAttributes audio = new AudioAttributes();
        audio.setCodec("libopencore_amrnb");
        audio.setBitRate(64000);
        audio.setChannels(1);
        audio.setSamplingRate(8000);
        //Encoding attributes
        EncodingAttributes attrs = new EncodingAttributes();
        attrs.setInputFormat("wav");
        attrs.setOutputFormat("amr");
        attrs.setAudioAttributes(audio);
        //Encode
        Encoder encoder = new Encoder();
        encoder.encode(new MultimediaObject(source), target, attrs);
        return target;
    }
}
